﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Xtreme_Server
{
    class FTPServer
    {
        private TcpListener _listener;

        public delegate void LogSentEventHandler(object sender, string log);
        public event LogSentEventHandler LogSent;

        public FTPServer(int port = 21, bool IPv6 = false, bool TLS = false)
        {
            if (!TLS)
                this.Port = port;
            else
                this.Port = 990;

            if(!IPv6)
                this._ip = IPAddress.Any;
            else
                this._ip = IPAddress.IPv6Any;
        }

        public FTPServer(IPAddress ip, int port = 21, bool TLS = false)
        {
            if (!TLS)
                this.Port = port;
            else
                this.Port = 990;

            this._ip = ip;
        }

        public int Port { get; set; }

        public bool TLS
        {
            get { return _tls; }
            set 
            {
                _tls = value;
                this.Stop();
                if (value) this.Port = 990; else this.Port = 21;
                this.Start();
            }
        }

        private bool _tls;

        public IPAddress IP { get { return _ip; } }

        private IPAddress _ip;

        private void SendLog(string l)
        {
            LogSent(this, l);
            /*LogSentEventHandler temp = LogSent;
            if (temp != null)
            {
                temp(this, l);
            }*/
        }

        public List<FTPUser> Users = new List<FTPUser>();

        public void Start()
        {
            try
            {
                SendLog("Starting Xtreme FTP Server v0.1...");
                _listener = new TcpListener(IP, Port);
                _listener.Start();
                SendLog("Server started at port " + Port + ".");
                _started = true;
                _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
            }
            catch (Exception e)
            {
                SendLog("An exception occured : " + e.Message);
            }
        }

        private bool _started;
        public bool Started { get { return _started; } }

        public void Stop()
        {
            if (_listener != null)
            {
                SendLog("Stopping Xtreme FTP Server v0.1...");
                _listener.Stop();
                SendLog("Server stopped.");
            }
        }

        private void HandleAcceptTcpClient(IAsyncResult result)
        {
            _listener.BeginAcceptTcpClient(HandleAcceptTcpClient, _listener);
            TcpClient client = _listener.EndAcceptTcpClient(result);

            SendLog("Client Connected.\r\n==================\r\nClient IP : " + client.Client.RemoteEndPoint);

            FTPUserConnection connection = new FTPUserConnection(client);

            

            ThreadPool.QueueUserWorkItem(connection.HandleClient, client);
        }
    }
}
